﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using CommDll;

namespace InstitutionAssitant
{
    /// <summary>
    /// ClassName:frmLocalStockIn
    /// Version:1.0
    /// Date:2019-06-04
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为本地入库数据处理
    /// </remarks>
    public partial class frmStockIn : DevComponents.DotNetBar.OfficeForm
    {
        #region 01.自定义属性
        int m_nFieldSelectedOrder = 1;  // 选择列所在顺序位置(0为起始列)
        public Boolean m_bIsSelectBegin = false;    //多选时用

        public string[] m_cbbData;  // 保存配送企业信息,过滤信息用
        public DataTable m_dt;  // 保存查询出来的数据
        #endregion
        public frmStockIn()
        {
            InitializeComponent();
        }

        #region 02.自定义方法

        /// <summary>
        /// 初始化DataGridView字段显示信息
        /// </summary>
        public void _initDgvTitle()
        {
            string strInfo = "";
            string strKind = this.Name;
            // 存在时不再处理
            if (AppConfig.m_dictFieldName.ContainsKey(strKind))
            {
                return;
            }
            strInfo = "ID-序号,StockInID-入库单编号,StockInDetailID-入库明细号,DeliveryCode-配送单号,DeliveryDetailCode-配送细单号,SupplierCode-客户编号,SupplierName-客户名称,DeliveryTime-配送时间,Code-药品编码,"
            + "Name-药品名称,RegistrationCode-批准文号,Form-剂型,Spec-规格,Manufacturer-生产厂家,BatchCode-生产批号,MakeDate-生产日期,ExpiredDate-生效日期,Unit-单位,"
            + "Price-单价,PurchasePrice-采购价,Amount-数量,PurchaseAmount-采购数,Money-金额,InvoiceNo-发票号,InvoiceDate-发票日期,UploadStatus-上传状态,UploadTime-上传时间,StockInTime-入库时间,WarehouseName-药库名称,PurchaseOrderCode-采购单号,PurchaseOrderDetailCode-采购细单号";
            AppConfig.m_dictFieldName.Add(strKind, strInfo);
            strInfo = "SourceID as ID,StockInID,StockInDetailID,DeliveryCode,DeliveryDetailCode,SupplierCode,SupplierName,DeliveryTime"
                    + ",Code,Name,RegistrationCode,Form,Spec,Manufacturer,BatchCode,MakeDate,ExpiredDate,Unit,Price,PurchasePrice,Amount,PurchaseAmount,Money,InvoiceNo,InvoiceDate,UploadStatus,UploadTime"
                    + ",StockInTime,WarehouseName,PurchaseOrderCode,PurchaseOrderDetailCode";            
            AppConfig.m_dictFieldSql.Add(strKind, strInfo);
            strInfo = "ID,StockInID,StockInDetailID,DeliveryCode,DeliveryDetailCode,SupplierCode,SupplierName,DeliveryTime"
                    + ",Code,Name,RegistrationCode,Form,Spec,Manufacturer,BatchCode,MakeDate,ExpiredDate,Unit,Price,PurchasePrice,Amount,PurchaseAmount,Money,InvoiceNo,InvoiceDate,UploadStatus,UploadTime"
                    + ",StockInTime,WarehouseName,PurchaseOrderCode,PurchaseOrderDetailCode";
            AppConfig.m_dictField0.Add(strKind, strInfo);
            strInfo = " 0,120,80,0,0,0,160,120,60,160,160,0,140,120,100,80,80,80,80,80,80,80,80,80,80,120,120,120,0,0";
            AppConfig.m_dictFieldWidth0.Add(strKind, strInfo);
            // 设置值
            strInfo = AppConfig.m_iniSetup._readValue("Common", string.Format("{0}_Field", strKind), AppConfig.m_dictField0[strKind]).Trim();
            AppConfig.m_dictField.Add(strKind, strInfo);
            strInfo = AppConfig.m_iniSetup._readValue("Common", string.Format("{0}_FieldWidth", strKind), AppConfig.m_dictFieldWidth0[strKind]).Trim();
            AppConfig.m_dictFieldWidth.Add(strKind, strInfo);
        }
        /// <summary>
        /// 初始化医疗机构信息
        /// </summary>
        private void _initCustomer()
        {
            // 01 取客户信息
            string strSql = "";
            strSql = "SELECT Name + '|' + Code as SupplierName FROM T_Base_SupplierCodeCheck where Code in (select SupplierCode from T_StockIn group by SupplierCode) order by ShowOrder desc";
            DataTable dt = DbHelper.DBAccess[0]._openSql_Dt(strSql);
            if (dt != null && dt.Rows.Count > 0)
            {
                FunctionComm._setInfoToCbb_Dt(cbbCustomer, dt);
                // 02 保存临时数据
                m_cbbData = new string[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    m_cbbData[i] = dt.Rows[i][0].ToString();
                }
            }
        }

        /// <summary>
        /// 取条件
        /// </summary>
        private Boolean _getQueryWhere(ref string strWhere)
        {
            Boolean bResult = true;
            string strTemp = "";            
            // 01 取客户名称
            if (cbCustomer.Checked)
            {
                strTemp = cbbCustomer.Text.Split('|').Length > 1 ? cbbCustomer.Text.Split('|')[1].Trim() : "";
                if (!strTemp.Equals(""))
                {
                    strWhere += (strWhere.Equals("") ? "" : " and ") + string.Format("SupplierCode = '{0}'", strTemp);
                }
            }
            // 取配送日期
            if (cbDate.Checked)
            {
                if (dtpDateBegin.Value.ToString("yyyy-MM-dd").CompareTo(dtpDateEnd.Value.ToString("yyyy-MM-dd")) > 0)
                {
                    CommFun._messageBoxShow(string.Format("起始日期大于终止日期！"), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                else
                {
                    strWhere += (strWhere.Equals("") ? "" : " and ") + string.Format("(DeliveryTime >= '{0}' and DeliveryTime <= '{1}')", dtpDateBegin.Value.ToString("yyyy-MM-dd"), dtpDateEnd.Value.AddDays(1).ToString("yyyy-MM-dd"));
                }
            }
            // 取入库日期
            if (cbStockInDate.Checked)
            {
                if (dtpStockInDateBegin.Value.ToString("yyyy-MM-dd").CompareTo(dtpStockInDateEnd.Value.ToString("yyyy-MM-dd")) > 0)
                {
                    CommFun._messageBoxShow(string.Format("入库起始日期大于终止日期！"), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                else
                {
                    strWhere += (strWhere.Equals("") ? "" : " and ") + string.Format("(StockInTime >= '{0}' and StockInTime <= '{1}')", dtpStockInDateBegin.Value.ToString("yyyy-MM-dd"), dtpStockInDateEnd.Value.AddDays(1).ToString("yyyy-MM-dd"));
                }
            }
            // 03 商品编号
            if (cbGoodsCode.Checked)
            {
                strWhere += (strWhere.Equals("") ? "" : " and ") + string.Format("(Code like '%{0}%')", txtGoodsCode.Text.Trim());
            }
            // 04 商品名称
            if (cbInvoiceNo.Checked)
            {
                strWhere += (strWhere.Equals("") ? "" : " and ") + string.Format("(InvoiceNo like '%{0}%')", txtInvoiceNo.Text.Trim());
            }
            // 04 发票号码
            if (cbGoodsName.Checked)
            {
                strWhere += (strWhere.Equals("") ? "" : " and ") + string.Format("(Name like '%{0}%')", txtGoodsName.Text.Trim());
            }
            // 05 入库状态
            if (cbStatus.Checked)
            {
                strWhere += (strWhere.Equals("") ? "" : " and ") + string.Format("(IsNull(UploadStatus,0) = {0})", rbStatus0.Checked ? 0 : 1);
            }
            // 06 入库编号
            if (cbStockInID.Checked)
            {
                strWhere += (strWhere.Equals("") ? "" : " and ") + string.Format("(StockInID like '%{0}%')", txtStockInID.Text.Trim());
            }
            // 08 入库库房
            if (cbWarehouse.Checked)
            {
                strWhere += (strWhere.Equals("") ? "" : " and ") + string.Format("WarehouseName = '{0}')", cbbWarehouse.Text.Split('|')[0].Trim());
            }
            bResult = true;
            return bResult;
        }

        /// <summary>
        /// 信息查询
        /// </summary>
        private void _query(Boolean bIsRefresh = true)
        {
            if (m_dt != null)
            {
                m_dt.Dispose();
            }
            // 00 取条件:有参数时按参数来取数据
            string strWhere = "";
            strWhere = " InstitutionCode = '{0}'";
            strWhere = string.Format(strWhere, AppInfo.m_UserInfo.m_strDeptCode);
            if (!_getQueryWhere(ref strWhere))
            {
                return;
            }
            if (!strWhere.Equals(""))
            {
                strWhere = string.Format(" where {0}", strWhere);
            }
            // 02 取数据
            string strSql = string.Format("SELECT {0} from T_StockIn {{0}} order by StockInTime desc", AppConfig.m_dictFieldSql[this.Name]);
            strSql = string.Format(strSql, strWhere);
            m_dt = DbHelper.DBAccess[0]._openSql_Dt(strSql);            
            // 更新数据
            if (bIsRefresh)
            {
                // 01 显示数据
                DataTable dt = m_dt.Copy();
                _showData(dt);
            }
        }

        /// <summary>
        /// 信息过滤
        /// </summary>
        private void _query0()
        {
            // 00 数据初始化
            DataTable dt = m_dt.Copy();
            DataView dv = dt.DefaultView;
            dv.RowFilter = "1<>1";  // 默认无数据
            // 01 取条件:有参数时按参数来取数据
            string strWhere = "";
            if (_getQueryWhere(ref strWhere))
            {
                dv.RowFilter = strWhere;
            }
            // 02显示数据
            _showData(dv.ToTable());
        }

        /// <summary>
        /// 数据显示
        /// </summary>
        /// <param name="dt"></param>
        private void _showData(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                // 显示数据
                // 0101 调整字段顺序
                CommFun._dataTableFieldMove(dt, AppConfig.m_dictField[this.Name]);
                // 0102 显示数据
                CommFun._showDataGridView0(dataGridViewX1, dt, AppConfig.m_dictFieldWidth[this.Name], AppConfig.m_dictFieldName[this.Name], true,true);
            }
            else
            {
                // 清除数据
                DataTable dtTemp = (DataTable)dataGridViewX1.DataSource;
                if (dtTemp != null && dtTemp.Rows.Count > 0)
                {
                    dtTemp.Rows.Clear();
                }
            }
            _GetTotalInfo();// lblRecord.Text = string.Format("提示:共有数据信息 {0} 项", dataGridViewX1 == null ? 0 : dataGridViewX1.Rows.Count);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void _init()
        {
            // 01 dataGridViewX1设为不可编辑
            dataGridViewX1.ReadOnly = true;
            dataGridViewX1.Anchor = (System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Top));
            // 02 初始化日期
            dtpDateBegin.Format = DateTimePickerFormat.Custom;
            dtpDateBegin.CustomFormat = "yyyy-MM-dd";
            dtpDateEnd.Format = dtpDateBegin.Format;
            dtpDateEnd.CustomFormat = dtpDateBegin.CustomFormat;
            dtpStockInDateBegin.Format = dtpDateBegin.Format;
            dtpStockInDateBegin.CustomFormat = dtpDateBegin.CustomFormat;
            dtpStockInDateEnd.Format = dtpDateBegin.Format;
            dtpStockInDateEnd.CustomFormat = dtpDateBegin.CustomFormat;

            dtpDateEnd.Value = DateTime.Now;
            dtpDateBegin.Value = dtpDateEnd.Value.AddDays(AppConfig.m_nDateBeforeDistribution * -1);
            dtpStockInDateEnd.Value = DateTime.Now;
            dtpStockInDateBegin.Value = dtpStockInDateEnd.Value.AddDays(AppConfig.m_nDateBeforeInvoice * -1);
            // 03 初始化配企
            _initCustomer();
            // 04 取库房信息
            FunctionComm._setInfoToCbb_Dt(cbbWarehouse, AppInfo.m_dtWarehouse);
            // 04 初始化DataGridView
            _initDgvTitle();
        }

        /// <summary>
        /// 设置全选或全不选
        /// </summary>
        /// <param name="nKind">1：全选 0：全不选</param>
        private void _SelectAll(int nKind)
        {
            if (dataGridViewX1.Rows.Count > 0)
            {
                this.dataGridViewX1.CurrentCell = null; // 设置当前列，因为如当前列在选择列时，会出现不能设置数据情况
                foreach (DataGridViewRow row in dataGridViewX1.Rows)
                {
                    row.Cells[m_nFieldSelectedOrder].Value = nKind;
                }
            }
        }

        /// <summary>
        /// 取选中的数据项信息,倒排方式,当nKind==0时,需去除未选数据
        /// </summary>
        /// <param name="nKind">0:去除未选项 1:取已先数据编号</param>
        private string _GetSelectedInfo(int nKind = 0)
        {
            // 01 去除未选项
            string strResult = "";
            string strID = "";
            for (int i = dataGridViewX1.Rows.Count - 1; i >= 0; i--)
            {
                strID = dataGridViewX1.Rows[i].Cells["IsSelected"].Value.ToString();
                if (!strID.Equals("1"))
                {
                    if (nKind == 0)
                    {
                        //0:去除未选项
                        dataGridViewX1.Rows.RemoveAt(i);
                    }
                }
                else
                {
                    strResult += (strResult.Equals("") ? "" : ",") + dataGridViewX1.Rows[i].Cells["ID"].Value.ToString();
                }
            }
            // 显示行号
            CommFun._formatHeaderCell(dataGridViewX1);
            _GetTotalInfo();// lblRecord.Text = string.Format("提示:共有数据信息 {0} 项", dataGridViewX1 == null ? 0 : dataGridViewX1.Rows.Count);
            return strResult;
        }

        /// <summary>
        /// 取汇总信息
        /// </summary>
        private void _GetTotalInfo()
        {
            double dTotal = 0;
            for (int i = dataGridViewX1.Rows.Count - 1; i >= 0; i--)
            {
                dTotal += FunctionComm._strToFloat(dataGridViewX1.Rows[i].Cells["Money"].Value.ToString());
            }
            lblRecord.Text = string.Format("提示:共有数据信息 {0} 项,合计:{1}元", dataGridViewX1 == null ? 0 : dataGridViewX1.Rows.Count, dTotal);          
        } 
        #endregion

        #region 03.系统事件方法

        private void frm_Load(object sender, EventArgs e)
        {
            _init();
        }

        private void cbbCustomer_DrawItem(object sender, DrawItemEventArgs e)
        {
            // 重绘使可以调整行距
            CommFun._cbbDrawItem(sender, e);
        }

        private void cbbCustomer_TextUpdate(object sender, EventArgs e)
        {
            CommFun._cbbFilter((ComboBox)sender, m_cbbData);
            Cursor = Cursors.Default;
        }

        private void cbCustomer_CheckedChanged(object sender, EventArgs e)
        {
            // 过滤数据
            if (m_dt == null || m_dt.Rows.Count < 1)
            {
                return;
            }
            Boolean bRefresh = true;
            Control control = (Control)sender;
            switch (control.Name)
            {
                case "cbbCustomer":
                    bRefresh = cbCustomer.Checked;
                    break;
                case "txtGoodsCode":
                    bRefresh = cbGoodsCode.Checked;
                    break;
                case "txtGoodsName":
                    bRefresh = cbGoodsName.Checked;
                    break;
                case "cbbWarehouse":
                    bRefresh = cbWarehouse.Checked;
                    break;
                case "txtInvoiceNo":
                    bRefresh = cbInvoiceNo.Checked;
                    break;
                case "rbStatus0":
                case "rbStatus1":
                    bRefresh = cbStatus.Checked;
                    break;
                case "dtpDateBegin":
                case "dtpDateEnd":
                    bRefresh = cbDate.Checked;
                    break;
                case "dtpInvoiceDateBegin":
                case "dtpInvoiceDateEnd":
                    bRefresh = cbStockInDate.Checked;
                    break;
            }
            if (bRefresh)
            {
                _query0();
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            _query();            
        }

        private void btnSelected_Click(object sender, EventArgs e)
        {
            _GetSelectedInfo();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (dataGridViewX1 != null && dataGridViewX1.Rows.Count > 0)
            {
                CommFun._ExportExcel("", dataGridViewX1);
            }
        }

        private void btnStockIn0_Click(object sender, EventArgs e)
        {
            string strInfoID = _GetSelectedInfo(1);            
            if (strInfoID.Equals(""))
            {
                CommFun._messageBoxShow("请先确定要取消入库的入库数据?", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult result = CommFun._messageBoxShow("是否确定取消入库的入库数据?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result != DialogResult.Yes)
            {
                return;
            }
            string strSql = "exec P_DeleteStockIn '{0}'";
            strSql = string.Format(strSql, strInfoID);
            bool bOK = DbHelper.DBAccess[0]._execSql(strSql) > 0;
            if (bOK)
            {
                // 去除数据
                for (int i = dataGridViewX1.RowCount - 1; i >= 0; i-- )
                {
                    DataGridViewRow dr = dataGridViewX1.Rows[i];
                    bOK = string.Format(",{0},", strInfoID).Contains(string.Format(",{0},", dr.Cells["ID"].Value.ToString()));
                    if (bOK)
                    {
                        dataGridViewX1.Rows.RemoveAt(i);
                    }
                }
                //foreach (DataGridViewRow dr in dataGridViewX1.Rows)
                //{
                //    bOK = string.Format(",{0},", strInfoID).Contains(string.Format(",{0},", dr.Cells["ID"].Value.ToString()));
                //    if (bOK)
                //    {
                //        dataGridViewX1.Rows.Remove(dr);
                //    }
                //}
                // 更新缓存数据
                _query(false);
                CommFun._messageBoxShow(string.Format("数据取消入库完成,共取消入库数据{0}条！", strInfoID.Equals("") ? 0 : strInfoID.Split(',').Length), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                CommFun._messageBoxShow(string.Format("本次数据入库取消失败！", ""), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnStockIn1_Click(object sender, EventArgs e)
        {
            string strInfoID = _GetSelectedInfo(1);
            if (strInfoID.Equals(""))
            {
                CommFun._messageBoxShow("请先确定要重新入库的数据?", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult result = CommFun._messageBoxShow("是否确定重新入库的所选数据?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result != DialogResult.Yes)
            {
                return;
            }
            string strSql = "exec P_RedoStockIn '{0}'";
            strSql = string.Format(strSql, strInfoID);
            bool bOK = DbHelper.DBAccess[0]._execSql(strSql) > 0;
            if (bOK)
            {
                // 更新数据
                strSql = string.Format("SELECT SourceID as ID,StockInID,StockInDetailID from T_StockIn where SourceID in ({0})", strInfoID);
                DataTable dt = DbHelper.DBAccess[0]._openSql_Dt(strSql);
                Dictionary<string,string> dictInfo = new Dictionary<string,string>();
                foreach(DataRow dr in dt.Rows){
                    dictInfo.Add(dr["ID"].ToString(), string.Format("{0}_{1}", dr["StockInID"].ToString(), dr["StockInDetailID"].ToString()));                    
                }
                DataTable dt1 = (DataTable)dataGridViewX1.DataSource;
                for (int i = 0;i< dt1.Rows.Count; i++)
                {
                    if (dictInfo.ContainsKey(dt1.Rows[i]["ID"].ToString()))
                    {
                        dt1.Rows[i]["StockInID"] = dictInfo[dt1.Rows[i]["ID"].ToString()].Split('_')[0];
                        dt1.Rows[i]["StockInDetailID"] = dictInfo[dt1.Rows[i]["ID"].ToString()].Split('_')[1];
                    }
                }                
                CommFun._messageBoxShow(string.Format("数据重新入库完成,共重新入库数据{0}条！", strInfoID.Equals("") ? 0 : strInfoID.Split(',').Length), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                CommFun._messageBoxShow(string.Format("本次数据重新入库失败！", ""), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion

        #region 04.GridViewX事件

        private void dataGridViewX1_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            // 选择列标题双击操作
            DataGridViewColumn newColumn = ((DataGridView)sender).Columns[e.ColumnIndex];
            if (e.ColumnIndex == m_nFieldSelectedOrder)
            {
                // 判定当前全选或未选状态
                if (newColumn.HeaderText.Contains("☑"))
                {
                    newColumn.HeaderText = "□";
                    _SelectAll(0);
                }
                else
                {
                    newColumn.HeaderText = "☑";
                    _SelectAll(1);
                }
            }
            else {
                // 排序
                CommFun._dgvColumnHeaderSort(sender, e); 
            }            
        }

        private void dataGridViewX1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            // 鼠标拖动多选时用:确定起始单元格
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                m_bIsSelectBegin = false;
                return;
            }
            // 只是单选框时作多项选择操作(鼠标按下不放多选)
            DataGridViewColumn newColumn = ((DataGridView)sender).Columns[e.ColumnIndex];
            // 判断是否可编辑列
            if (newColumn.DataPropertyName.Equals("IsSelected"))
            {
                m_bIsSelectBegin = true;
            }
            else {
                m_bIsSelectBegin = false;
            }
        }

        private void dataGridViewX1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            // 鼠标拖动多选时用:确定结束单元格
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                m_bIsSelectBegin = false;
                return;
            }
            // 只是单选框时作多项选择操作(鼠标按下不放多选)
            DataGridViewColumn newColumn = ((DataGridView)sender).Columns[e.ColumnIndex];
            // 判断是否可编选择列
            if (newColumn.DataPropertyName.Equals("IsSelected") && m_bIsSelectBegin)
            {
                // 处理所选择列为选中状态
                foreach (DataGridViewCell item in ((DataGridView)sender).SelectedCells)
                {
                    if (item.ColumnIndex == m_nFieldSelectedOrder) {
                        if (((item.Value == null) ? "0" : item.Value.ToString()) == "1")
                        {
                            item.Value = 0;
                        }
                        else
                        {
                            item.Value = 1;
                        }
                    }
                }
            }
        }
                
        private void dataGridViewX1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            // 单元格双击操作，对于可编辑的列进行编辑状态，否则不能进行编辑,显示详细信息
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }
            // 只是单选框时作多项选择操作
            DataGridViewColumn newColumn = ((DataGridView)sender).Columns[e.ColumnIndex];
            // 判断是否可编选择列
            if (!newColumn.DataPropertyName.Equals("IsSelected"))
            {
                frmInfoDetail frm = new frmInfoDetail();
                frm.m_nRow = e.RowIndex;
                frm.m_dgv = ((DataGridView)sender);
                frm.ShowDialog();
            }
        }

        private void dataGridViewX1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // 改变行颜色
            CommFun._dgvRowEnter(sender, e);
        }

        private void dataGridViewX1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // 设置标题信息 
            if (e.Button == MouseButtons.Right)
            {
                frmDataShowSetup frm = new frmDataShowSetup();
                frm.m_strInfoKind = this.Name;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    if (frm.m_bIsSaveWidth)
                    {
                        // 保存宽度
                        AppConfig._saveDgvWidth(dataGridViewX1, frm.m_strInfoKind, true);
                        CommFun._messageBoxShow("显示宽度保存成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        if (dataGridViewX1.Rows.Count > 0)
                        {
                            btnQuery_Click(null, null);
                        }
                    }
                }
            }
        }

        #endregion

        private void frmStockIn_Load(object sender, EventArgs e)
        {
            _init();
        }
    }
}